/*
 * Copyright 2002-2015 the Beijing Brain Technology co.,LTD http://www.bris.cn/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bris.niosocket.message;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * 定义流水号基本规则与方法</br>
 * 2015年11月27日</br>
 * @author xunaidong
 * @since 1.0.0
 */
public abstract class SerialNumber {
	
	private static final Logger log = LoggerFactory.getLogger(SerialNumber.class);

	/**
	 * 外部指定流水号
	 * 
	 * @param number
	 *
	 */
	public SerialNumber(String number) {
		this.number = number;
	}

	public SerialNumber() {

	}
	protected String number;
	
    /**
     * 流水号规则长度
     * 
     * @return 流水号的长度
     */
   public abstract int length();

    /**
     * 获取流水号
     * @return 流水号
     */
   public String value(){
		if (number == null || number.length() != length()) {
			synchronized (this) {
				generate();
			}
			log.debug("流水号为空或长度不满足{},正在自动生成流水号:{}",length(),number);
			return this.value();
		}
		return number;
   }
    /**
     * 
     * 产生一个流水号<br/> number
     * 
     * @author xunaidong 
     * @return 
     * @since 1.0.0
     */
   protected abstract void generate();
    
}
